package com.paomi.client.activity.address.presenter;

import android.app.Activity;
import android.app.Dialog;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter;
import com.hannesdorfmann.mosby3.mvp.MvpView;
import com.paomi.client.R;
import com.paomi.client.activity.address.View.AddAddressView;
import com.paomi.client.activity.address.View.CityBean;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.net.Api;
import com.paomi.client.net.BaseCallback;
import com.paomi.client.net.Constants;

import java.lang.reflect.Field;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddAddressPre extends MvpBasePresenter<AddAddressView> {

    /**
     * 添加地址
     */
    public void addAddress(Map<String, String> map) {
        Api.getInstanceService(Constants.User_URL).createAddress(map).enqueue(new BaseCallback<BaseJSON>() {
            @Override
            public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                getView().addAddressSuccess(result);
            }
        });
    }


    public void setNumberPickerDividerColor(NumberPicker numberPicker, Activity activity) {
        NumberPicker picker = numberPicker;
        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
        for (Field pf : pickerFields) {
            if (pf.getName().equals("mSelectionDivider")) {
                pf.setAccessible(true);
                try {
                    //设置分割线的颜色值
                    pf.set(picker, new ColorDrawable(activity.getResources().getColor(R.color.line_color)));
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (Resources.NotFoundException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }

    /**
     * 获取省
     */
    public void getProvince(String pid) {
        Api.getInstanceService(Constants.BASE_URL).getRegionAll(pid).enqueue(new BaseCallback<CityBean>() {
            @Override
            public void onResponse(Call<CityBean> call, CityBean result) {
                getView().showProvince(result);
            }
        });
    }

    /**
     * 获取市
     */
    public void getCity(String pid) {
        Api.getInstanceService(Constants.BASE_URL).getRegionAll(pid).enqueue(new BaseCallback<CityBean>() {
            @Override
            public void onResponse(Call<CityBean> call, CityBean result) {
                getView().showCity(result);
            }
        });
    }

    /**
     * 获取区
     */
    public void getArea(String pid) {
        Api.getInstanceService(Constants.BASE_URL).getRegionAll(pid).enqueue(new BaseCallback<CityBean>() {
            @Override
            public void onResponse(Call<CityBean> call, CityBean result) {
                getView().showArea(result);
            }
        });
    }
    /**
     * 地址删除的操作
     *
     * @param activity
     */
    public void deleteBtn(Activity activity, String id) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.dialog_delete_shaow, null);
        final Dialog dialog = new Dialog(activity, R.style.theme_dialog);
        dialog.setContentView(view);
        dialog.show();
        TextView title = view.findViewById(R.id.title);
        title.setText(activity.getResources().getString(R.string.comment_delete));
        TextView msg_tv = view.findViewById(R.id.msg_tv);
        msg_tv.setText(activity.getResources().getString(R.string.address_delete));
        TextView textCancel = view.findViewById(R.id.btn_qd);
        textCancel.setText(activity.getResources().getString(R.string.no_delete));
        textCancel.setTextColor(activity.getResources().getColor(R.color.text_gray_99));
        textCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView textSure = view.findViewById(R.id.btn_success);
        textSure.setText(activity.getResources().getString(R.string.comment_delete));
        textSure.setTextColor(activity.getResources().getColor(R.color.color_ff0000));
        textSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                deleteAddress(id);
            }
        });
    }
    /**
     * 保存的操作
     *
     * @param activity
     */
    public void saveBtn(Activity activity, Map<String, String> map) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.dialog_delete_shaow, null);
        final Dialog dialog = new Dialog(activity, R.style.theme_dialog);
        dialog.setContentView(view);
        dialog.show();
        TextView title = view.findViewById(R.id.title);
        title.setText(activity.getResources().getString(R.string.save_sure));
        TextView msg_tv = view.findViewById(R.id.msg_tv);
        msg_tv.setText(activity.getResources().getString(R.string.save_edit));
        TextView textCancel = view.findViewById(R.id.btn_qd);
        textCancel.setText(activity.getResources().getString(R.string.save_cancel));
        textCancel.setTextColor(activity.getResources().getColor(R.color.text_gray_99));
        textCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView textSure = view.findViewById(R.id.btn_success);
        textSure.setText(activity.getResources().getString(R.string.save_sure));
        textSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                addAddress(map);
            }
        });
    }

    /**
     * 删除地址
     */
    public void deleteAddress(String id) {
        Api.getInstanceService(Constants.User_URL).addressDelete(id).enqueue(new BaseCallback<BaseJSON>() {
            @Override
            public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                getView().deleteSuccess(result);
            }
        });
    }
}
